package com.pp.util.mqtt.yun;

import com.aliyuncs.AcsResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.RpcAcsRequest;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.iot.model.v20180120.*;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.apache.tomcat.util.codec.binary.Base64;

/**
 * @author 张子艺
 * @packge com.pp.util.mqtt.yun
 * @data 2019-03-25 16:12
 * @project ppProject
 */
public class YunTest {
    public static void main(String[] args) throws ClientException {
     /*   String accessKey = "LTAI12tJ9yxQmJ2H";
        String accessSecret = "YK5DOThA33XSVXLfWlq5eyF7PxoAPs";
        DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Iot", "iot.cn-shanghai.aliyuncs.com");
        IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKey, accessSecret);
        DefaultAcsClient client = new DefaultAcsClient(profile); //初始化SDK客户端


        PubRequest request = new PubRequest();
        request.setProductKey("a1LM9XwLXIq");
        request.setMessageContent(Base64.encodeBase64String("!!!!!!!!!!!!!!!!!!!!!!!!".getBytes()));
        //request.setTopicFullName("/a1LM9XwLXIq/gExKTRvmrtZIiKGciW5H/get");
        request.setTopicFullName("/a1LM9XwLXIq/gExKTRvmrtZIiKGciW5H/user/get");
        request.setQos(0); //目前支持QoS0和QoS1
        try
        {
            PubResponse response = client.getAcsResponse(request);
            System.out.println(response.getSuccess());
            System.out.println(response.getErrorMessage());
        }
        catch (ServerException e)
        {
            e.printStackTrace();
        }
        catch (ClientException e)
        {
            e.printStackTrace();
        }*/
        // createProductTest("你好",0);
        //  deleteProductTest("a103bXa7SkC");
        //getDeviceStatus();
       // sendMsg();
        setModel();
    }


    /**
     * 发送消息RRPC
     */
    public static Boolean sendMsg() {
        RRpcRequest request = new RRpcRequest();
        request.setDeviceName("gExKTRvmrtZIiKGciW5H");
        request.setProductKey("a1LM9XwLXIq");
        request.setTimeout(5000);
        request.setRequestBase64Byte(Base64.encodeBase64String("{\"data\":\"!!!!!!!!!!!!!!!!!!!!!!!!\"".getBytes()));
        RRpcResponse response = (RRpcResponse) executeTest1(request);
        if (response != null && response.getSuccess() != false) {
            System.err.println(response.getRrpcCode());
            return response.getSuccess();
        } else {
            System.out.println("失败！requestId:" + response.getRequestId() + "原因:" + response.getErrorMessage());
        }
        return false;
    }


    /**
     * 设置设备属性
     */
    public static Boolean setModel() {
        SetDevicePropertyRequest request = new SetDevicePropertyRequest();
        request.setDeviceName("gExKTRvmrtZIiKGciW5H");
        request.setProductKey("a1LM9XwLXIq");
        request.setItems("{\"Status\":1}");
        SetDevicePropertyResponse response = (SetDevicePropertyResponse) executeTest1(request);
        if (response != null && response.getSuccess() != false) {
            System.err.println(response.getData().getMessageId());
            return response.getSuccess();
        } else {
            System.out.println("失败！requestId:" + response.getRequestId() + "原因:" + response.getErrorMessage());
        }
        return false;
    }

    /**
     * 创建产品
     *
     * @param productName 产品名称
     * @param nodeType    产品描述
     * @return 产品的PK
     */
    public static String createProductTest(String productName, Integer nodeType) {
        CreateProductRequest request = new CreateProductRequest();
        request.setProductName(productName);

        request.setNodeType(nodeType);//节点的类型，0标识设备，1标识网关
        CreateProductResponse response = (CreateProductResponse) executeTest1(request);
        if (response != null && response.getSuccess() != false) {
            System.out.println("创建产品成功！productKey:" + response.getProductKey());
            return response.getProductKey();
        } else {
            System.out.println("创建产品失败！requestId:" + response.getRequestId() + "原因:" + response.getErrorMessage());
        }
        return null;
    }

    /**
     * 删除产品
     *
     * @param productKey 产品key
     * @return 产品的PK，可以在管理门户获取
     */
    public static Boolean deleteProductTest(String productKey) {
        DeleteProductRequest request = new DeleteProductRequest();
        request.setProductKey(productKey);
        DeleteProductResponse response = (DeleteProductResponse) executeTest1(request);
        if (response != null && response.getSuccess() != false) {
            System.out.println("删除产品成功！productKey:" + response.getSuccess());
            return response.getSuccess();
        } else {
            System.out.println("创建产品失败！requestId:" + response.getRequestId() + "原因:" + response.getErrorMessage());
        }
        return false;
    }

    /**
     * 查看指定设备的运行状态。
     * 设备状态。取值：
     * ONLINE：设备在线。
     * OFFLINE：设备离线。
     * UNACTIVE：设备未激活。
     * DISABLE：设备已禁用。
     */
    public static Boolean getDeviceStatus() {
        GetDeviceStatusRequest request = new GetDeviceStatusRequest();
        request.setDeviceName("gExKTRvmrtZIiKGciW5H");
        request.setProductKey("a1LM9XwLXIq");
        GetDeviceStatusResponse response = (GetDeviceStatusResponse) executeTest1(request);
        if (response != null && response.getSuccess() != false) {
            System.out.println("调用" + response.getSuccess());

            System.out.println("状态" + response.getData().getStatus());
            return response.getSuccess();
        } else {
            System.out.println("查看失败！requestId:" + response.getRequestId() + "原因:" + response.getErrorMessage());
        }
        return false;
    }

    public static AcsResponse executeTest1(RpcAcsRequest request) {

        DefaultAcsClient client = null;
        String regionId = "cn-shanghai";
        String accessKeyID = "LTAI12tJ9yxQmJ2H";
        String accessKeySecret = "YK5DOThA33XSVXLfWlq5eyF7PxoAPs";
        String productCode = "Iot";
        String domain = "iot.cn-shanghai.aliyuncs.com";
        IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyID, accessKeySecret);
        try {
            DefaultProfile.addEndpoint(regionId, regionId, productCode, domain);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        // 初始化client
        client = new DefaultAcsClient(profile);

        System.out.println("client 初始化完成！");

        AcsResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (Exception e) {
            System.out.println("执行失败：e:" + e.getMessage());
        }
        return response;
    }
}
